package com.cxl.service.impl;

import com.cxl.entity.City;
import com.cxl.mapper.CityMapper;
import com.cxl.service.ICityService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 崔学磊
 * @since 2022-03-16
 */
@Service
public class CityServiceImpl extends ServiceImpl<CityMapper, City> implements ICityService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private CityMapper cityMapper;

    @Override
    public List<City> cityList() {
        List<City> cities = null;
        if (redisTemplate.hasKey("citys")) {
            cities = redisTemplate.opsForList().range("citys", 0, -1);
        } else {
            cities = cityMapper.findAll();
            redisTemplate.opsForList().rightPushAll("citys", cities);
        }
        return cities;
    }
}
